package com.bao.work.work01;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @author baoye
 * @date 2024-08-13/17:06
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
        Document document = DocumentHelper.createDocument();
        Element bookstore = document.addElement("bookstore");
        Element book = bookstore.addElement("book");
        book.addAttribute("category","科幻");
        Element title = book.addElement("title");
        title.addAttribute("lang","english");
        title.addText("harryPotter");
        book.addElement("author").addText("rowling");
        book.addElement("year").addText("2000");
        book.addElement("price").addText("29.99");

        Element book2 = bookstore.addElement("book");
        book2.addAttribute("category","it");
        Element title2 = book2.addElement("title");
        title2.addAttribute("lang","chinese");
        title2.addText("java");
        book2.addElement("author").addText("james gosling");
        book2.addElement("year").addText("1995");
        book2.addElement("price").addText("50");

        OutputFormat outputFormat=OutputFormat.createPrettyPrint();
        outputFormat.setEncoding("UTF-8");

        XMLWriter xmlWriter=new XMLWriter(new FileWriter("day15/src/com/bao/work/work01/bookstore.xml"),outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    }
}
